package org.csu.mypetstore.api.persistence;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.reflection.wrapper.BaseWrapper;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.Order;
import org.csu.mypetstore.api.vo.OrderVO;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Repository
public interface OrderMapper extends BaseMapper<Order> {
//    @Select("SELECT o.*, li.linenum, li.quantity, li.itemid, li.unitprice " +
//            "FROM orders o " +
//            "LEFT JOIN lineitem li ON o.orderid = li.orderid " +
//            "WHERE o.userid = #{userId}")

    @Select("SELECT o.*, li.linenum, li.quantity, li.itemid, li.unitprice, o.orderdate " +
            "FROM orders o " +
            "LEFT JOIN lineitem li ON o.orderid = li.orderid " +
            "WHERE o.userid = #{userId}")
    @Results({
            @Result(property = "orderId", column = "orderid"),
            @Result(property = "orderdate", column = "orderdate"), // 映射 orderdate
            @Result(property = "linenum", column = "linenum"),
            @Result(property = "quantity", column = "quantity"),
            @Result(property = "itemId", column = "itemid"),
            @Result(property = "unitPrice", column = "unitprice")
    })
    List<OrderVO> selectOrdersWithItemsByUserId(String userId);

    @Select("SELECT o.*, li.linenum, li.quantity, li.itemid, li.unitprice, o.orderdate " +
            "FROM orders o " +
            "LEFT JOIN lineitem li ON o.orderid = li.orderid " +
            "WHERE o.userid = #{userId}")
    @Results({
            @Result(property = "orderId", column = "orderid"),
            @Result(property = "orderdate", column = "orderdate"), // 映射 orderdate
            @Result(property = "linenum", column = "linenum"),
            @Result(property = "quantity", column = "quantity"),
            @Result(property = "itemId", column = "itemid"),
            @Result(property = "unitPrice", column = "unitprice")
    })

    OrderVO selectOrderWithItemsById(Integer orderId);
}
